"""
职位推荐Agent
根据简历内容，调用RAG检索并生成推荐理由
"""
from rag_system.retriever import RAGRetriever
from .deepseek_client import DeepSeekClient

class RecruiterAgent:
    def __init__(self, rag_retriever: RAGRetriever):
        self.rag_retriever = rag_retriever
        self.deepseek_client = DeepSeekClient()

    def recommend_jobs(self, resume_content: str, n_results: int = 5):
        # 1. 检索匹配职位
        job_matches = self.rag_retriever.retrieve_job_matches(resume_content, n_results)
        # 2. 生成推荐理由
        for job in job_matches:
            try:
                job['reason'] = self.deepseek_client.generate_job_recommendation(
                    resume_content, job['content']
                )
            except Exception as e:
                # 如果DeepSeek调用失败，使用备用方法
                job['reason'] = self._generate_fallback_reason(resume_content, job['content'])
        return job_matches

    def _generate_fallback_reason(self, resume_content: str, job_content: str) -> str:
        # 备用方法：提取技能/关键词重叠
        resume_keywords = set(resume_content.split())
        job_keywords = set(job_content.split())
        overlap = resume_keywords & job_keywords
        if overlap:
            return f"你的简历中提到的技能与该职位需求高度相关：{'、'.join(list(overlap)[:5])}"
        else:
            return "你的背景与该职位有一定相关性，建议进一步了解。" 